import unittest
from api.login import LoginAPI
from parameterized import parameterized
from utils import Base_dir
import json


def build_data():
    test_data = []
    json_dir = Base_dir + "/data/login.json"
    with open(json_dir, encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            login_json = case_data.get("login_data")
            status_code = case_data.get("status_code")
            success = case_data.get("success")
            code = case_data.get("code")
            message = case_data.get("message")
            test_data.append((login_json, status_code, success, code, message))
            print("test_data={}".format((login_json, status_code, success, code, message)))
    return test_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()

    @parameterized.expand(build_data)
    def test02_login(self, login_json, status_code, success, code, message):
        rep = self.login_api.login(login_json)
        self.assertEqual(status_code, rep.status_code)
        self.assertEqual(success, rep.json().get("success"))
        self.assertEqual(code, rep.json().get("code"))
        self.assertIn(message, rep.json().get("message"))
